// pages/homepage/homepage.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      "../../image/zaixian@2x.png",
      "../../image/dianhua1@2x.png",
      "../../image/chanping@2x.png",
      "../../image/ditu@2x.png",
    ],
    current: 0,
    image1: 'shouye1@2x.png',
    image2: 'phone@2x.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      winHeight: app.globalData.winHeight,
      imageUrl: app.globalData.imageUrl
    })

    getBanner(this);
    getAllGoods(this);
    getPhone(this)
  },
  tabHandle: function (e) {
    var that = this;
    if (e.target.id == 1) {
      wx.makePhoneCall({
        phoneNumber: '' + that.data.shopPhone,
      })
    } else if (e.target.id == 2) {
        wx.navigateTo({
          url: '../../pages/moreProduct/moreProduct',
        })
    } else if (e.target.id == 3) {
         wx.openLocation({
           latitude: 30.693514,
           longitude: 103.944376,
         })
    }
  },
  switchs: function (e) {
    if (e.target.id == 0) {
      this.setData({
        current: e.target.id,
        image1: 'shouye1@2x.png',
        image2: 'phone@2x.png',
      })
    } else if (e.target.id == 1) {
      this.setData({
        current: e.target.id,
        image1: 'shouye@2x.png',
        image2: 'dianhua@2x.png',
      })
      wx.makePhoneCall({
        phoneNumber: '' + this.data.shopPhone,
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  , toDetail:function(e){
    wx.navigateTo({
      url: '../../pages/productDetail/productDetail?id='+e.target.id,
    })
  }
})


function getBanner(that) {
  wx.request({
    url: app.globalData.allUrl + "/user/image",
    data: {
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      that.setData({
        imgUrls: res.data
      })
    }
  })
}

function getAllGoods(that) {
  wx.request({
    url: app.globalData.allUrl + "/goods/index",
    data: {
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
    //  console.log(res.data)
      that.setData({
        allGoods: res.data
      })
    }
  })
}

function getPhone(that) {
  wx.request({
    url: app.globalData.allUrl + "/user/get_phone",
    data: {
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (res) {
      that.setData({
        shopPhone: res.data[0].phone
      })
    }
  })
}